#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <time.h>
#include <errno.h>
#include <string.h>


/*
// sysinfo结构保持了系统启动后的信息，主要包括启动到现在的时间，可用内存空间、共享内存空间、进程的数目等。
// sysinfo中的uptime可以计算出系统的启动时间。

struct sysinfo 
{
	long uptime;             // Seconds since boot 
	unsigned long loads[3];  // 1, 5, and 15 minute load averages 
    unsigned long totalram;  // Total usable main memory size 
    unsigned long freeram;   // Available memory size 
    unsigned long sharedram; // Amount of shared memory 
    unsigned long bufferram; // Memory used by buffers 
    unsigned long totalswap; // Total swap space size 
    unsigned long freeswap;  // swap space still available 
    unsigned short procs;    // Number of current processes 
    char _f[22];             // Pads structure to 64 bytes 
};

*/


unsigned int get_timezone()
{
	unsigned int timezone = -1;
	time_t time_local;
	time_t time_utc;
	struct tm *tm_local; 
	struct tm *tm_utc;

	time(&time_local);
	time(&time_utc);
	//time_utc = time_local;
	
	tm_local = localtime(&time_local);
	time_local = mktime(tm_local);

	tm_utc = gmtime(&time_utc);
	time_utc = mktime(tm_utc);
	
	timezone = (time_local - time_utc)/3600;

	return timezone;
}

void SetEnv()
{
    char *p = getenv("USER");
    if (p != NULL)
    {   
        printf("USER = %s\n", p); 
    }   

    if (0 != setenv("TZ", "Aisa/Shanghai", 0)) 
    {   
        printf("[%s:%d] setenv failed\n", __FUNCTION__, __LINE__);
    }   
}       

static int print_system_boot_time()
{
    struct sysinfo info;
    time_t cur_time = 0;
    time_t boot_time = 0;
    struct tm *ptm = NULL;

    if (sysinfo(&info)) 
	{
    	fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",	errno, strerror(errno));
   		return -1;
    }
    
	time(&cur_time);
    if (cur_time > info.uptime) 
	{
   		boot_time = cur_time - info.uptime;
    }
    else 
	{
    	boot_time = info.uptime - cur_time;
    }
    
	time(&boot_time);
    ptm = gmtime(&boot_time); //此函数返回的时间日期未经时区转换，而是UTC 时间
	printf("System boot time: %d-%-d-%d %02d:%02d:%02d\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
	
	time_t tt;
	time(&tt);	
    ptm = localtime(&tt); //此函数返回的时间日期未经时区转换，而是UTC 时间
	printf("Local time: %d-%-d-%d %02d:%02d:%02d\n", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
   
	return 0; 
}


int main(int argc, char *argv[])
{
    struct sysinfo info;
    char run_time[128] = { 0 };

    if (sysinfo(&info)) 
	{
        fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",errno, strerror(errno));
        return -1;
    }

    long timenum = info.uptime;
    int runday = timenum/86400;
    int runhour = (timenum%86400)/3600;
    int runmin = (timenum%3600)/60;
    int runsec = timenum%60;
    
	sprintf(run_time,"SystemRunTime：%d Day(s)%d Hour(s)%d Min%d S",runday,runhour,runmin,runsec);
    printf("%s\n",run_time);
    
	if (print_system_boot_time() != 0) 
	{
		printf("[%s:%d] get system boot time failed\n", __FUNCTION__, __LINE__);
    	return -1;
    }

	printf("Time zone:%d\n", get_timezone());

    return 0; 
}
